package com.jumpy.entities;

import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.jumpy.MovingRect;

public class Character extends AnimatedSprite {

	public static final float MAX_VELOCITY = 800.0f;
	public static final float MIN_VELOCITY = 250.0f;

	private boolean moving;
	private float until;

	// private final PhysicsHandler mPhysicsHandler;

	public Character(final float pX, final float pY,
			final TiledTextureRegion pTextureRegion,
			final VertexBufferObjectManager pVertexBufferObjectManager) {
		super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
		// this.mPhysicsHandler = new PhysicsHandler(this);
		// this.registerUpdateHandler(this.mPhysicsHandler);
		// this.mPhysicsHandler.setVelocity(Character.DEMO_VELOCITY, 0);
	}

	@Override
	protected void onManagedUpdate(final float pSecondsElapsed) {

		if (moving == true) {
			float distancia = Math.abs(this.getX() - this.until);

			if (this.getX() < this.until)
				this.setX(this.getX()
						+ Math.max(
								((Character.MAX_VELOCITY) * distancia / MovingRect.CAMERA_WIDTH),
								Character.MIN_VELOCITY) * pSecondsElapsed);
			else
				this.setX(this.getX()
						- Math.max(
								((Character.MAX_VELOCITY) * distancia / MovingRect.CAMERA_WIDTH),
								Character.MIN_VELOCITY) * pSecondsElapsed);
		}

		super.onManagedUpdate(pSecondsElapsed);
	}

	public void moveUntil(float xPos) {
		this.moving = true;
		this.until = xPos;
	}

	public void stop() {
		this.moving = false;
	}

}